﻿using Castle.DynamicProxy; 

namespace DynamicProxyMode;

/// <summary>
/// 动态代理模式
/// 依赖 Castle.Core
/// </summary>
public  class DynamicProxyModeClass
{
    public  void testMain()
    {
        var generator = new ProxyGenerator();
        var target = new Water();
        var proxy = generator.CreateClassProxyWithTarget(target,new IInterceptor1(), new IInterceptor2());
        proxy.Invoke();
    }
}

public interface IWater
{
    void Invoke();
}
public class Water: IWater
{
    public virtual void Invoke()
    {
        Console.WriteLine("DoSome->success");
    }
}
public class IInterceptor1 : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        Console.WriteLine("Aop--11111");
        invocation.Proceed();
        Console.WriteLine("Aop--22222");
    }
}

public class IInterceptor2 : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        Console.WriteLine("Aop--33333");
        invocation.Proceed();
        Console.WriteLine("Aop--44444");
    }
}